<?php

class Controller_Page extends Controller_Site {

    public function action_links() {
        $links = ORM::factory('page')->where('name', '=', 'links')->find();
        $this->content->set('links', $links);
    }

    public function action_contact() {
        $contact = ORM::factory('page')->where('name', '=', 'contact')->find();
        $this->content->set('contact', $contact);
    }

    public function action_index() {
        $home = ORM::factory('page')->where('name', '=', 'home')->find();
        $this->content->set('home', $home);
        
        $last_news = ORM::factory('news')->order_by('id', 'DESC')->limit('3')->find_all();
        $this->content->set('last_news', $last_news);
    }

    public function action_edit() {
        if (Session::instance()->get('admin') != '1') {
            $this->request->redirect('page/home');
        } else {
            if ($this->request->method() == 'POST') {
                try {
                    $page = ORM::factory('page')->where('name', '=', 'home')->find();
                    $page->body = $this->request->post('home');
                    $page->save();

                    $page = ORM::factory('page')->where('name', '=', 'contact')->find();
                    $page->body = $this->request->post('contact');
                    $page->save();

                    $page = ORM::factory('page')->where('name', '=', 'links')->find();
                    $page->body = $this->request->post('links');
                    $page->save();
                    
                    $page = ORM::factory('page')->where('name', '=', 'participate_form')->find();
                    $page->body = $this->request->post('participate');
                    $page->save();

                    $this->setFlash('notice', 'Sikeresen módosítottad az oldalakat!');
                    $this->request->redirect('admin/tasks');
                } catch (ORM_Validation_Exception $e) {
                    $form->set('errors', $e->errors('models'));
                }
            }

            $this->content->set('ckeditor', $ckeditor)->set('config', $config);
        }
    }

}

?>
